package com.niiwoo.civet.mobile.lanmao.handler;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.niiwoo.civet.account.dto.request.UpdateGroupAccountCorrelateRequestDTO;
import com.niiwoo.civet.account.dto.response.UpdateGroupRegisterOrderResponseDTO;
import com.niiwoo.civet.account.enums.GroupAccountCorrelateOrderStatusEnum;
import com.niiwoo.civet.account.service.AccountRegisterDubboService;
import com.niiwoo.tripod.lanmao.enums.AuditStatusEnum;
import com.niiwoo.tripod.lanmao.enums.LanMaoGatewayServiceNameEnum;
import com.niiwoo.tripod.provider.exception.BizException;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 集团账户关联回调处理
 */
@Component
public class GroupAccountCorrelateNotifyHandler implements LanMaoNotifyHandler {

    public static final Logger logger = LoggerFactory.getLogger(GroupAccountCorrelateNotifyHandler.class);

    @Reference(version = "1.0.0")
    AccountRegisterDubboService accountRegisterDubboService;

    @Override
    public boolean supports(String serviceName) {
         return LanMaoGatewayServiceNameEnum.GROUP_ACCOUNT_AUTH.name().equals(serviceName);
    }

    @Override
    public HandleResult handle(JSONObject json) {
        logger.info("集团账户回调###报文###data={}", json.toJSONString());
        GroupAccountCorrelateGatewayResponse response = json.toJavaObject(GroupAccountCorrelateGatewayResponse.class);
        HandleResult result = new HandleResult();
        try {
            UpdateGroupAccountCorrelateRequestDTO updateRequestDto = new UpdateGroupAccountCorrelateRequestDTO();
            updateRequestDto.setRequestNo(response.getRequestNo());
            updateRequestDto.setAccountNo(response.getPlatformUserNo());
            updateRequestDto.setErrorCode(response.getErrorCode());
            updateRequestDto.setErrorMessage(response.getErrorMessage());
            updateRequestDto.setGroupAccountNo(response.getGroupAccountNo());
            updateRequestDto.setIsBizSuccess(response.isBusinessSuccess() && response.isCallSuccess());
            logger.info("集团账户回调###更新请求###request={}", JSON.toJSONString(updateRequestDto));
            UpdateGroupRegisterOrderResponseDTO updateRegisterOrderResponseDTO = accountRegisterDubboService.updateGroupAccountCorrelateOrder(updateRequestDto);
            GroupAccountCorrelateGatewayResponseData data = new GroupAccountCorrelateGatewayResponseData();
            data.setOrderStatus(updateRegisterOrderResponseDTO.getOrderStatus().name());
            result.setNeedAck(true);
            result.setRequestNo(response.getRequestNo());
            result.setData(data);
        } catch (BizException e) {
            logger.error("集团账户回调###异常###errorCode={}, errorMessage={}", e.getErrorCode(), e.getMessage());
            throw e;
        } catch (Exception e) {
            logger.error(String.format("集团账户回调###未知异常###errorMessage=%s", e.getMessage()), e);
            throw new BizException("SYS9999");
        }
        logger.info("集团账户回调###更新响应###response={}", JSON.toJSONString(result));
        if (!response.isCallSuccess() || !response.isBusinessSuccess()) {
            logger.info("集团账户回调###存管请求处理失败###返回存管错误提示, requestNo={}, errorCode={}, errorMessage={}", response.getRequestNo(), response.getErrorCode(), response.getErrorMessage());
            throw new BizException(response.getErrorCode(), response.getErrorMessage());
        }
        return result;
    }

    @Setter
    @Getter
    public static class GroupAccountCorrelateGatewayResponseData {

        private String orderStatus;

    }

    @Setter
    @Getter
    public static class GroupAccountCorrelateGatewayResponse {

        private static final String CALL_SUCCESS_CODE = "0";
        private static final String BUSINESS_SUCCESS_CODE = "SUCCESS";

        // 调用状态(0 为调用成功、1 为失败,返回 1 时请看【调用失败错误码】及错误码)
        private String code;

        // 业务处理状态(处理失败 INIT;处理成功 SUCCESS),平台可根据非 SUCCESS 状态做相应处理,处理失败时可参考错误码
        private String status;
        // 错误码
        private String errorCode;
        // 错误码
        private String errorMessage;
        //平台用户编号
        private String platformUserNo;

        // 请求流水号
        private String requestNo;

        // 集团账户编号
        private String groupAccountNo;

        public boolean isCallSuccess() {
            return StringUtils.isNotBlank(code) && CALL_SUCCESS_CODE.equals(code);
        }

        public boolean isBusinessSuccess() {
            return StringUtils.isNotBlank(status) && BUSINESS_SUCCESS_CODE.equals(status);
        }


    }

}
